package com.jxc.action.rkgl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jxc.action.BaseAction;
import com.jxc.aop.LogInfoAop;
import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.User;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.Json;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VOrderlist;
import com.jxc.model.vo.VOrderlistItem;
import com.jxc.model.vo.VSerial;
import com.jxc.model.vo.VUser;
import com.jxc.service.DeliverlistItemServiceI;
import com.jxc.service.DeliverlistServiceI;
import com.jxc.service.OrderServiceI;
import com.jxc.service.SerialService;
import com.jxc.service.UserServiceI;
import com.jxc.util.ExceptionUtil;
import com.jxc.util.ResourceUtil;
import com.opensymphony.xwork2.ModelDriven;

@Namespace("/rkgl")
@Action(value = "orderAction", results = {
		@Result(name = "order", location = "/order/order.jsp"),
		@Result(name = "orderAdd", location = "/order/orderAdd.jsp"),
		@Result(name = "orderInfo", location = "/order/orderInfo.jsp") })
public class OrderAction extends BaseAction implements ModelDriven<VOrderlist> {
	private final static Logger logger = Logger.getLogger(OrderAction.class);
	@Autowired
	private OrderServiceI orderService;
	@Autowired
	private UserServiceI userService;
	@Autowired
	private BaseDaoI<User> userDao;
	
	@Autowired
	private SerialService serialService;
	@Autowired
	private DeliverlistServiceI deliverlistService;

	private VOrderlist model = new VOrderlist();
	private String items;
	
	@Override
	public VOrderlist getModel() {
		return model;
	}
	
	public String getItems() {
		return items;
	}
	public void setItems(String items) {
		this.items = items;
	}

	public String order() {
		getSubAuths("RKGL_RKGL");
		return "order";
	}
	public void noAuth_datagrid() {
		try {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext
			.getRequest().getSession().getAttribute(
					ResourceUtil.getSessionInfoName());
			VUser user = userService.get(sessionInfo.getUserId());
			if(user.getReid()!=null){
				model.setRid(user.getReid());
				DataGrid subDg = orderService.datagrid(model);
				this.writeJson(subDg);
			}else {
				model.setUpid(user.getId());
				DataGrid subDg = orderService.datagrid(model);
				this.writeJson(subDg);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	public String orderAdd() {
		return "orderAdd";
	}
	

	@LogInfoAop(info = "收货批量入库")
	public void deliverAddOrder() {
		Json json = new Json();
		try {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
			String s = model.getItems();
			JSONArray array = JSONArray.parseArray(s);
			for(int i = 0; i < array.size();i++){
				JSONObject jobj = (JSONObject)array.get(i);
				VOrderlist vorder = JSON.toJavaObject(jobj,VOrderlist.class);
				VSerial vs = new VSerial();
				vs.setRid(vorder.getRid().intValue());
				vs.setType("RK");
				vorder.setSerial(serialService.getAll(vs));
				vorder.setDate(new Date());
				vorder.setUid(sessionInfo.getUserId());
				orderService.save(vorder, vorder.getOrderItems());
			}
			String dlis = model.getDeliverlistItems();
			deliverlistService.distributeUpdate(dlis);
			json.setMsg("添加成功！");
			json.setSuccess(true);
		} catch (Exception e) {
			json.setMsg("添加失败！");
			e.printStackTrace();
			logger.info(ExceptionUtil.getExceptionMessage(e));
		}
		super.writeJson(json);
	}
	
	@LogInfoAop(info = "商品入库")
	public void add() {
		Json j = new Json();
		
		items=ServletActionContext.getRequest().getParameter("items");
	
		if(items==null){
			
			j.setMsg("items 为空！");
			j.setSuccess(false);
			super.writeJson(j);
			
			return;
		}
		
		try {
			String ba = new String(model.getRemark().getBytes("ISO-8859-1"),"utf-8");
			model.setRemark(ba);
			
			orderService.save(model,items);
			j.setMsg("添加成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg("添加失败！");
			logger.info(ExceptionUtil.getExceptionMessage(e));
		}
		super.writeJson(j);
	}

	public void delete() {
		Json j = new Json();
		orderService.delete(model.getIds());
		j.setSuccess(true);
		j.setMsg("删除成功!");
		super.writeJson(j);
	}
	
	public String orderInfo(){
		getRequest().setAttribute("id", model.getId());
		return "orderInfo";
	}
	
	public void noAuth_checkOrder(){
		writeJson(orderService.search(model));
	}
	
	public void noAuth_checkItems(){
		writeJson(orderService.datagridItems(model));
	}
}
